/******************************************************************************

	COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2008, 2009, 2010.

	This file is part of Maxsi Engine.

	Maxsi Engine is free software: you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or (at your
	option) any later version.

	Maxsi engine is distributed in the hope that it will be useful, but WITHOUT
	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
	FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
	for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with Maxsi Engine. If not, see <http://www.gnu.org/licenses/>.

	MaxsiEngine.dll
	A general purpose C++ library for the Maxsi Technology Project.

	MaxsiEngineDLL.h
	Macros and systems to make it easier to link to the Maxsi Engine.

******************************************************************************/

#ifdef _MSC_VER

#include "MaxsiEngine.h"

#pragma warning(disable: 4995)

BeginMaxsiNamespace

DLL::DLL( void )
{
	Executable = NULL;
	DLLPath = 0;
}

DLL::~DLL( void )
{
	Unload();
	delete DLLPath;
}


void* DLL::ProcAddress ( LPCSTR lpProcName )
{
	return GetProcAddress(Executable,lpProcName);
}

bool DLL::Import()
{
	return Import(DLLPath);
}

bool DLL::Import(char* LibraryPath)
{
	if ( Executable ) { return true; }
	if ( !LibraryPath ) { return false; }

	Executable = LoadLibraryA(LibraryPath);

	if (Executable == NULL)
	{
		/*LPVOID lpMsgBuf;
		LPVOID lpDisplayBuf;
		DWORD dw = GetLastError(); 

		FormatMessage(
			FORMAT_MESSAGE_ALLOCATE_BUFFER | 
			FORMAT_MESSAGE_FROM_SYSTEM |
			FORMAT_MESSAGE_IGNORE_INSERTS,
			NULL,
			dw,
			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
			(LPTSTR) &lpMsgBuf,
			0, NULL );

		// Display the error message and exit the process

		lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
			(lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)"DLL::Import")+40)*sizeof(TCHAR)); 
		StringCchPrintf((LPTSTR)lpDisplayBuf, 
			LocalSize(lpDisplayBuf) / sizeof(TCHAR),
			TEXT("%s failed with error %d: %s"), 
			TEXT("DLL::Import"), dw, lpMsgBuf); 
		MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); 

		LocalFree(lpMsgBuf);
		LocalFree(lpDisplayBuf);*/

		//char*		Error	=	BuildString(2,
		//	"An error happened in DLL:Import in MaxsiEngineDLL.cpp in MaxsiEngine.dll\n\n"
		//	"Couldn't load library: ",LibraryPath);


		//MessageBoxA(NULL,Error,"Error",MB_ICONERROR);

		//delete[]	Error;

		return false;
	}

	if ( DLLPath ) { delete DLLPath; }
	DLLPath = new char[strlen(LibraryPath)+1];

	strcpy_s(DLLPath,strlen(LibraryPath)+1, LibraryPath);
	return true;
}

bool DLL::Unload()
{
	if ( !Executable ) { return true; }

	return (FreeLibrary(Executable)==TRUE);
}

EndMaxsiNamespace

#endif
